package com.my.meitianbackup.crawler;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.plugin.activerecord.Db;
import com.my.meitianbackup.model.Meitian;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

public class TaoKeZhuShouNewCrawler implements PageProcessor {
	private static Logger logger = LoggerFactory.getLogger("TaoKeZhuShouCrawler");
	private Site site = Site.me()// .setHttpProxy(new
									// HttpHost("127.0.0.1",8888))
			.setRetryTimes(3).setSleepTime(500).setUseGzip(true);

	private static String[] cates = { "全部", "女装", "男装", "内衣", "数码家电", "美食",
			"美妆个护", "母婴", "鞋包配饰", "家居家装", "文体车品", "其他" };
	
	private static String[] catesEn = { "quanbu", "nvzhuang", "nanzhuang", "neiyi", "shumajiadian", "meishi",
			"meizhuanggehu", "muyin", "xiebaopeishi", "jiajujiazhuang", "wentichepin", "qita" };
	
	private static int[] catePage = {0,50,0,0,0,0,30,50,50,0,0,0};

	public void process(Page page) {
		try {
			List<String> hrefs = page.getHtml().xpath("//div[@class=\"product-title\"]/a/@href").all();
			
			List<String> picUrls1 = page.getHtml().xpath("//div[@class=\"goodsimg-link\"]/a/img/@src").all();
			List<String> picUrls2 = page.getHtml().xpath("//div[@class=\"goodsimg-link\"]/a/img/@data-original").all();
			
			List<String> titles = page.getHtml().xpath("//div[@class=\"product-title\"]/a/@title").all();
			List<String> infos = page.getHtml().xpath("//p[@class=\"wenan-text\"]/text()").all();
			//*[@id="index"]/div[3]/div[1]/ul/li[2]/a
			String cate = page.getHtml().xpath("//li[@class=\"li-active\"]/a/text()").toString();
			String cateEn = "";
			for(int i=0; i<cates.length; i++) {
				if(cates[i].equals(cate)) {
					cateEn = catesEn[i];
				}
			}
			//System.out.println("hrefs:"+hrefs);
			//System.out.println("cateEn:"+cateEn);
			//System.out.println("titles:"+titles);
			//System.out.println("infos:"+infos);
			if(cateEn.equals("")) {
				System.out.println("return");
				return;
			}
			List<Meitian> updateList = new ArrayList<Meitian>();
			for(int i=0; i<hrefs.size(); i++) {
				String href = hrefs.get(i).split("\\?")[0];
				Long id = Long.parseLong(href.split("/detail/")[1]);
				Meitian meitian = Meitian.dao.findById(id);
				if(meitian != null && meitian.getGoodName() !=null && !meitian.getGoodName().equals("")) {
					meitian.setHasInfo(1);
					meitian.setInfo(infos.get(i));
					meitian.setTitle(titles.get(i));
					if(!picUrls1.get(i).equals("")) {
						meitian.setTaokePicUrl(picUrls1.get(i));
					} else if(!picUrls2.get(i).equals("")){
						meitian.setTaokePicUrl(picUrls2.get(i));
					} else {
						continue;
					}
					meitian.setCate(cate);
					meitian.setCateEn(cateEn);
					updateList.add(meitian);
				}
			}
			if(updateList.size() != 0) {
				System.out.println(updateList.get(0).getCate() +" - "+updateList.get(0).getCateEn()+ ": " + updateList);
				int[] updateResult = Db.batchUpdate(updateList, updateList.size());
				System.out.println("updateResult: "+Arrays.toString(updateResult));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	public Site getSite() {
		return site;
	}

	public void runCrawler() {
		Spider spider = Spider.create(new TaoKeZhuShouNewCrawler()).thread(2);
		List<String> list = new ArrayList<String>();
		
		String urlTemplate ="http://www.taokezhushou.com/?cid=%s&sort=new&page=";
		for(int i=1; i<cates.length; i++) {
			for(int j=1; j<=catePage[i]; j++) {
				list.add(String.format(urlTemplate, i)+j);
			}
		}
		//System.out.println(list.toString());
		spider.<ResultItems>getAll(list);
		spider.close();
	}

}
